package com.attendance.service.impl;

import com.attendance.common.BusinessException;
import com.attendance.dto.request.RoleRequest;
import com.attendance.entity.Role;
import com.attendance.mapper.RoleMapper;
import com.attendance.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Override
    public void createRole(RoleRequest request) {
        // 检查角色编码是否已存在
        if (lambdaQuery().eq(Role::getCode, request.getCode()).count() > 0) {
            throw new BusinessException("角色编码已存在");
        }

        Role role = new Role();
        role.setName(request.getName());
        role.setCode(request.getCode());
        role.setDescription(request.getDescription());
        role.setStatus(request.getStatus());
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        save(role);
    }

    @Override
    public void updateRole(RoleRequest request) {
        Role role = getById(request.getId());
        if (role == null) {
            throw new BusinessException("角色不存在");
        }

        // 检查新的角色编码是否与其他角色冲突
        if (!role.getCode().equals(request.getCode()) && 
            lambdaQuery()
                .eq(Role::getCode, request.getCode())
                .ne(Role::getId, request.getId())
                .count() > 0) {
            throw new BusinessException("角色编码已存在");
        }

        role.setName(request.getName());
        role.setCode(request.getCode());
        role.setDescription(request.getDescription());
        role.setStatus(request.getStatus());
        role.setUpdateTime(LocalDateTime.now());
        updateById(role);
    }

    @Override
    public void deleteRole(Long id) {
        removeById(id);
    }

    @Override
    public Role getRole(Long id) {
        return getById(id);
    }

    @Override
    public Page<Role> listRoles(Integer pageNum, Integer pageSize, String keyword) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like(Role::getName, keyword)
                  .or()
                  .like(Role::getCode, keyword)
                  .or()
                  .like(Role::getDescription, keyword);
        }
        
        wrapper.orderByDesc(Role::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public void enableRole(Long id) {
        Role role = getById(id);
        if (role == null) {
            throw new BusinessException("角色不存在");
        }
        
        // 检查是否已经是启用状态
        if (role.getStatus() == 1) {
            throw new BusinessException("角色已经是启用状态");
        }

        // 更新状态为启用
        Role updateRole = new Role();
        updateRole.setId(id);
        updateRole.setStatus(1);
        updateRole.setUpdateTime(LocalDateTime.now());
        updateById(updateRole);
    }

    @Override
    public void disableRole(Long id) {
        Role role = getById(id);
        if (role == null) {
            throw new BusinessException("角色不存在");
        }

        // 检查是否是系统内置角色
        if ("ADMIN".equals(role.getCode()) || "USER".equals(role.getCode())) {
            throw new BusinessException("系统内置角色不能禁用");
        }
        
        // 检查是否已经是禁用状态
        if (role.getStatus() == 0) {
            throw new BusinessException("角色已经是禁用状态");
        }

        // 更新状态为禁用
        Role updateRole = new Role();
        updateRole.setId(id);
        updateRole.setStatus(0);
        updateRole.setUpdateTime(LocalDateTime.now());
        updateById(updateRole);
    }

    @Override
    public List<Role> listEnabledRoles() {
        return lambdaQuery()
            .eq(Role::getStatus, 1)
            .orderByDesc(Role::getCreateTime)
            .list();
    }
} 